package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.SurveyContentDao;
import dao.SurveyItemsDao;
import entity.SurveyContent;
import entity.SurveyItems;

/**
 * Servlet implementation class GetSurveyItems
 */
@WebServlet("/GetSurveyItems")
public class GetSurveyItems extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetSurveyItems() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SurveyItemsDao sid=new SurveyItemsDao();       //调查项
		SurveyContentDao scd=new SurveyContentDao();   //调查项细节
		List<SurveyItems> list=sid.getAll();          
		List<SurveyContent> list2=scd.getAll();
		String si="[";
		String sc;             //调查细节
		for (SurveyItems s: list) {
		 si+="{\"id\":"+s.getId()+",\"name\":\""+s.getName()+"\",\"contents\":";
		 sc="["; 
			for (SurveyContent s2 : list2) {
				if(s.getId()==s2.getItemId()){
			      sc+="{\"id\":"+s2.getId()+",\"content\":\""+s2.getContent()+"\",\"itemId\":"+s2.getItemId()+"},";
				}
			}
			sc=sc.substring(0, sc.length()-1);
			sc+="]},";
			response.setCharacterEncoding("utf-8");
			si+=sc;
		}
		si=si.substring(0, si.length()-1);
		si+="]";
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		out.print(si);
//		System.out.println("******");
//		System.out.println(si);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
